<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/icons/favicon-16x16.png?v=2.8.0" type="image/png" sizes="16x16"><link rel="icon" href="/images/icons/favicon-32x32.png?v=2.8.0" type="image/png" sizes="32x32"><meta name="description" content="Buffer的数据读取                           flip()方法(buffer数据切换读模式)          123456789101112131415161718&#x2F;&#x2F;1.创建一个指定长度的缓冲区ByteBuffer allocate &#x3D; ByteBuffer.allocate(10);allocate.put(&quot;0">
<meta property="og:type" content="article">
<meta property="og:title" content="rpc架构设计">
<meta property="og:url" content="https://lou_jianyang.gitee.io/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/index.html">
<meta property="og:site_name" content="ljyxsg的技术成长录">
<meta property="og:description" content="Buffer的数据读取                           flip()方法(buffer数据切换读模式)          123456789101112131415161718&#x2F;&#x2F;1.创建一个指定长度的缓冲区ByteBuffer allocate &#x3D; ByteBuffer.allocate(10);allocate.put(&quot;0">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://lou_jianyang.gitee.io/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/buffer%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96flip1.png">
<meta property="og:image" content="https://lou_jianyang.gitee.io/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/buffer%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96clear2.png">
<meta property="article:published_time" content="2023-01-28T14:10:20.000Z">
<meta property="article:modified_time" content="2023-02-02T09:08:20.652Z">
<meta property="article:author" content="ljyxsg">
<meta property="article:tag" content="ljyxsg的技术成长录">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://lou_jianyang.gitee.io/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/buffer%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96flip1.png"><title>rpc架构设计 | ljyxsg的技术成长录</title><link ref="canonical" href="https://lou_jianyang.gitee.io/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.8.0"><link rel="stylesheet" href="css/custom.css"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 6.3.0"></head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/about/"><span class="header-nav-menu-item__icon"><i class="fas fa-address-card"></i></span><span class="header-nav-menu-item__text">关于</span></a></div></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">ljyxsg的技术成长录</div><div class="header-banner-info__subtitle">亿点点nice的ljy</div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">rpc架构设计</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2023-01-28</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2023-02-02</span></span></div></header><div class="post-body">
        <h1 id="Buffer的数据读取"   >
          <a href="#Buffer的数据读取" class="heading-link"><i class="fas fa-link"></i></a><a href="#Buffer的数据读取" class="headerlink" title="Buffer的数据读取"></a>Buffer的数据读取</h1>
      
        <h3 id="flip-方法-buffer数据切换读模式"   >
          <a href="#flip-方法-buffer数据切换读模式" class="heading-link"><i class="fas fa-link"></i></a><a href="#flip-方法-buffer数据切换读模式" class="headerlink" title="flip()方法(buffer数据切换读模式)"></a>flip()方法(buffer数据切换读模式)</h3>
      <!-- ![buffer数据读取flip1](uploads\rpc\buffer数据读取flip1.png) -->
<img src="/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/buffer%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96flip1.png" class="" title="buffer数据读取flip1">

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1.创建一个指定长度的缓冲区</span></span><br><span class="line"><span class="type">ByteBuffer</span> <span class="variable">allocate</span> <span class="operator">=</span> ByteBuffer.allocate(<span class="number">10</span>);</span><br><span class="line">allocate.put(<span class="string">&quot;0123&quot;</span>.getBytes());</span><br><span class="line">System.out.println(<span class="string">&quot;position:&quot;</span> + allocate.position()); <span class="comment">//4</span></span><br><span class="line">System.out.println(<span class="string">&quot;limit:&quot;</span> + allocate.limit());<span class="comment">//10</span></span><br><span class="line">System.out.println(<span class="string">&quot;capacity:&quot;</span> +allocate.capacity());<span class="comment">//10</span></span><br><span class="line">System.out.println(<span class="string">&quot;remaining:&quot;</span> + allocate.remaining()); <span class="comment">//6</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//2.切换读模式</span></span><br><span class="line">System.out.println(<span class="string">&quot;数据读取--------------&quot;</span>);</span><br><span class="line">allocate.flip();</span><br><span class="line">System.out.println(<span class="string">&quot;position:&quot;</span> + allocate.position()); <span class="comment">//0</span></span><br><span class="line">System.out.println(<span class="string">&quot;limit:&quot;</span> + allocate.limit());<span class="comment">//4</span></span><br><span class="line">System.out.println(<span class="string">&quot;capacity:&quot;</span> +allocate.capacity());<span class="comment">//10</span></span><br><span class="line">System.out.println(<span class="string">&quot;remaining:&quot;</span> + allocate.remaining()); <span class="comment">//4</span></span><br><span class="line"><span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; allocate.limit(); i++) &#123;</span><br><span class="line">System.out.println(allocate.get());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>




        <h3 id="clear-方法-buffer数据切换写模式"   >
          <a href="#clear-方法-buffer数据切换写模式" class="heading-link"><i class="fas fa-link"></i></a><a href="#clear-方法-buffer数据切换写模式" class="headerlink" title="clear()方法(buffer数据切换写模式)"></a>clear()方法(buffer数据切换写模式)</h3>
      <!-- ![buffer数据读取clear2.png](uploads\rpc\buffer数据读取clear2.png) -->
<img src="/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/buffer%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96clear2.png" class="" title="buffer数据读取clear2">

<figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//切换写模式</span></span><br><span class="line">System.out.println(<span class="string">&quot;切换写模式-----------------&quot;</span>);</span><br><span class="line">allocate.clear();<span class="comment">//position=0;limit=capacity</span></span><br><span class="line">allocate.put(<span class="string">&quot;abc&quot;</span>.getBytes());</span><br><span class="line">System.out.println(<span class="keyword">new</span> <span class="title class_">String</span>(allocate.array()));</span><br></pre></td></tr></table></div></figure>

</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ 本文结束，感谢您的阅读 ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">本文作者: </span><span class="copyright-author__value"><a href="https://lou_jianyang.gitee.io">ljyxsg</a></span></div><div class="copyright-link"><span class="copyright-link__name">本文链接: </span><span class="copyright-link__value"><a href="https://lou_jianyang.gitee.io/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/">https://lou_jianyang.gitee.io/2023/01/28/rpc%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">版权声明: </span><span class="copyright-notice__value">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！</span></div></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/2023/01/29/idea%E5%BF%85%E8%A3%85%E7%9A%84%E6%8F%92%E4%BB%B6/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">idea必装的插件</span></a></div><div class="paginator-next"><a class="paginator-next__link" href="/2023/01/27/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80%E5%92%8Cjava%E8%AF%AD%E8%A8%80%E7%89%B9%E6%80%A7/"><span class="paginator-prev__text">java面试汇总</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">文章目录</span><span class="sidebar-nav-ov">站点概览</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Buffer%E7%9A%84%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%8F%96"><span class="toc-number">1.</span> <span class="toc-text">
          Buffer的数据读取</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#flip-%E6%96%B9%E6%B3%95-buffer%E6%95%B0%E6%8D%AE%E5%88%87%E6%8D%A2%E8%AF%BB%E6%A8%A1%E5%BC%8F"><span class="toc-number">1.0.1.</span> <span class="toc-text">
          flip()方法(buffer数据切换读模式)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#clear-%E6%96%B9%E6%B3%95-buffer%E6%95%B0%E6%8D%AE%E5%88%87%E6%8D%A2%E5%86%99%E6%A8%A1%E5%BC%8F"><span class="toc-number">1.0.2.</span> <span class="toc-text">
          clear()方法(buffer数据切换写模式)</span></a></li></ol></li></ol></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/images/icons/stun-logo.svg" alt="avatar"></div><p class="sidebar-ov-author__text">Hello Stun</p></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/archives/"><div class="sidebar-ov-state-item__count">15</div><div class="sidebar-ov-state-item__name">归档</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/categories/"><div class="sidebar-ov-state-item__count">9</div><div class="sidebar-ov-state-item__name">分类</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/tags/"><div class="sidebar-ov-state-item__count">2</div><div class="sidebar-ov-state-item__name">标签</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">你已阅读了 </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2023</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>ljyxsg</span></div><div><span>由 <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a> 强力驱动</span><span> v6.3.0</span><span class="footer__devider">|</span><span>主题 - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.8.0</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script src="/js/utils.js?v=2.8.0"></script><script src="/js/stun-boot.js?v=2.8.0"></script><script src="/js/scroll.js?v=2.8.0"></script><script src="/js/header.js?v=2.8.0"></script><script src="/js/sidebar.js?v=2.8.0"></script></body></html>